package com.zhiyou.app.common.util;

import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
@Component
public class JsonFileReader {

    @javax.annotation.Resource
    private ResourceLoader resourceLoader;

    /**
     * Read JSON file
     *
     * @param fileName the name of the JSON file
     * @return the JSON object
     */
    public JSONObject readJsonFile(String fileName) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // Load the JSON file using ResourceLoader
            Resource resource = resourceLoader.getResource("classpath:/workflow/" + fileName + ".json");
            InputStream inputStream = resource.getInputStream();

            // Assuming the JSON file contains a Map
            return objectMapper.readValue(inputStream, JSONObject.class);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return null;
    }
}

